package cn.iocoder.yudao.module.picturebook.controller.admin.audio;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.picturebook.controller.admin.audio.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.audio.AudioDO;
import cn.iocoder.yudao.module.picturebook.service.audio.AudioService;

@Tag(name = "绘本系统 - 绘本全音频")
@RestController
@RequestMapping("/picturebook/audio")
@Validated
public class AudioController {

    @Resource
    private AudioService audioService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本全音频")
    @PreAuthorize("@ss.hasPermission('picturebook:audio:create')")
    public CommonResult<Integer> createAudio(@Valid @RequestBody AudioSaveReqVO createReqVO) {
        return success(audioService.createAudio(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本全音频")
    @PreAuthorize("@ss.hasPermission('picturebook:audio:update')")
    public CommonResult<Boolean> updateAudio(@Valid @RequestBody AudioSaveReqVO updateReqVO) {
        audioService.updateAudio(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本全音频")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:audio:delete')")
    public CommonResult<Boolean> deleteAudio(@RequestParam("id") Integer id) {
        audioService.deleteAudio(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘本全音频")
                @PreAuthorize("@ss.hasPermission('picturebook:audio:delete')")
    public CommonResult<Boolean> deleteAudioList(@RequestParam("ids") List<Integer> ids) {
        audioService.deleteAudioListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本全音频")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:audio:query')")
    public CommonResult<AudioRespVO> getAudio(@RequestParam("id") Integer id) {
        AudioDO audio = audioService.getAudio(id);
        if (audio == null) {
            return success(null);
        }
        return success(BeanUtils.toBean(audio, AudioRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本全音频分页")
    @PreAuthorize("@ss.hasPermission('picturebook:audio:query')")
    public CommonResult<AudioVO> getAudioPage(@Valid AudioPageReqVO pageReqVO) {
        AudioVO pageResult = audioService.getAudioPage(pageReqVO);
        return success(pageResult);
    }
    //获取收藏的音频
    @GetMapping("/getFavoritesAudio")
    @Operation(summary = "获取收藏的音频")
    public CommonResult<AudioVO> getFavoritesAudio(@RequestHeader("token") Long userId) {
        AudioVO favoritesAudio = audioService.getFavoritesAudio(userId);
        return success(favoritesAudio);
    }
    //根据大计划id获取计划内容音频
    @GetMapping("/getPlanContentAudio")
    @Operation(summary = "根据大计划id获取计划内容音频")
    public CommonResult<AudioVO> getPlanContentAudio(@RequestParam("planId") Long planId) {
        AudioVO planContentAudio = audioService.getPlanContentAudio(planId);
        return success(planContentAudio);
    }
}